import http from '../../apis/api.js';
import allFacilities from '../../utils/facilities.js';
import util from '../../utils/util.js';
const app = getApp();
let self;
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    loginInfo:null,
    msgImge:'/images/msg.png',
    coloect1: '/images/collect1.png',
    coloect2: '/images/collect2.png',
    isShowta:'',/** */
    isCollect:false,/**是否收藏 */
    storeNo: '',
    userImage:'',
    storeIntros:null,
    storeDtl:null,
    vouchers:[],/**代金券列表 */
    selectVoucher:{},
    swiperConf:{
      autoplay:true,
      interval:3000,
      circular:true,
      current:0,
      showCurrent:1,
    },
    isShowFacilities:false,
    allFacilities,
    mapConfig:{
      longitude:'',/**中心经度 */
      latitude: '',/**中心纬度 */
      hasMarkers:false,
      scale:16,
      markers:[{
        id: 0,
        latitude: '',
        longitude: '',
        title:''
      }],
      enableZoom:false,
      enableScroll:false,
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self = this;
    self.data.storeNo=options.storeNo;
    self.data.swiperConf.current = 0;
    self.data.swiperConf.showCurrent = 1;
    http.browseStore(`?storeNo=${self.data.storeNo}`)//民宿浏览接口
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    self.data.swiperConf.current = 0;
    self.data.swiperConf.showCurrent = 1;
    util.getUserInfo().then(res => {
      self.data.userImage = res.userInfo.avatarUrl;
      self.setData({
        userImage: self.data.userImage,
        swiperConf:self.data.swiperConf
      })
    })
    
    self.getCollectInfo();
  
    http.storeIntros({//获取民宿介绍
      storeNo: self.data.storeNo,
      introType:'50800104'
    }).then(res => {
      self.data.storeIntros = res.data[0].contect.replace(/\<img/gi,'<img style="width:100%;height:auto;display:block;margin: 0 auto;"');
      self.setData({
        storeIntros: self.data.storeIntros
      })
    })
    http.storeDtl({//获取民宿基本信息
      storeNo: self.data.storeNo
    }).then(res => {
      // 格式化名宿标记
      res.data.storeLableList = res.data.storeLable.split(',');
      let facilities = res.data.facilities.split(",");
      res.data.facilitiesList = [];
      res.data.picList=[];
      res.data.storePics.forEach(el=>{
        if (el.picType =="51000102"){
          res.data.picList.push(el.inPath);
        } 
      })
      self.data.allFacilities.forEach(el => {
        facilities.forEach(item => {
          if (el.name.indexOf(item) > -1) {
            res.data.facilitiesList.push(el)
          }
        })
      })
      self.data.mapConfig.latitude = res.data.latitude;
      self.data.mapConfig.longitude = res.data.longitude;
      self.data.mapConfig.markers[0].latitude = res.data.latitude;
      self.data.mapConfig.markers[0].longitude = res.data.longitude;
      self.data.mapConfig.markers[0].title = res.data.address;
      self.data.mapConfig.hasMarkers=true;
      self.data.storeDtl = res.data;
      //格式化名宿设施
      self.setData({
        storeDtl: self.data.storeDtl,
        mapConfig: self.data.mapConfig
      })
    })
    // 获取民宿代金券
    http.getProdByStoreNo({ storeNo: self.data.storeNo }).then(res => {
      self.data.vouchers = res.data;
      self.data.selectVoucher=res.data[0];
      self.setData({
        vouchers: self.data.vouchers,
        selectVoucher: self.data.selectVoucher
      })
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    return {
      url:self.data.storeDtl.storePics[0].inPath,
      title: self.data.storeDtl.storeName ,
      path: `/pages/detail/detail?storeNo=${self.data.storeNo}`
    }
  },
  getCollectInfo() {//获取收藏信息
    wx.getStorage({
      key: 'loginInfo',
      success: function (res) {
        if (res.data.id) {
          self.data.loginInfo = res.data;
          http.collectorInfo({
            custNo: self.data.loginInfo.custNo,
            storeNo: self.data.storeNo
          }).then(result => {
            if (result.data) {
              self.data.isCollect = true;
            } else {
              self.data.isCollect = false;
            }
            self.setData({
              isCollect: self.data.isCollect
            })
          })
        }
      },
      fail() {
        self.data.loginInfo = null;
      }
    })
  },
  gotohere(e){
    wx.showLoading({
      title: '打开内置地图...',
    })
    wx.openLocation({//​使用微信内置地图查看位置。
      latitude: parseFloat(self.data.mapConfig.latitude),//要去的纬度-地址
      longitude: parseFloat(self.data.mapConfig.longitude),//要去的经度-地址
      name: self.data.mapConfig.markers.title,
      address: self.data.mapConfig.markers.title,
      success(res){
        wx.hideLoading()
      },
      fail(res){
        wx.hideLoading();
        wx.showToast({
          title: '内置地图打开失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  orderNow(){
    wx.navigateTo({
      url: '/pages/order/order',
    })
  },
  swiperChange(e){/**切换民宿图片 */
    self.data.swiperConf.current = e.detail.current;
    self.data.swiperConf.showCurrent = e.detail.current+1;
    self.setData({
      swiperConf: self.data.swiperConf
    })
  },
  handleTap(e){
    let data = e.currentTarget.dataset.btntype;
    switch (data){
      case "phone":
        wx.makePhoneCall({
          phoneNumber: self.data.storeDtl.serTel
        })
      break;
      case "collect":
        if(self.data.loginInfo){
          if(self.data.isCollect){
            http.cancelColStore({
              custNo: self.data.loginInfo.custNo,
              storeNo: self.data.storeNo
            }).then(res => {
              if (res.code == 0) {
              wx.showToast({
                title: '取消收藏成功！',
                icon: 'success',
                duration: 2000
              })
                self.getCollectInfo();
              }else{
                wx.showToast({
                  title: '取消收藏失败！',
                  icon: 'none',
                  duration: 2000
                })
              }
            })
          }else{
            http.colStore({
              custNo: self.data.loginInfo.custNo,
              storeNo: self.data.storeNo
            }).then(res => {
              if(res.code==0){
                wx.showToast({
                  title: '收藏成功！',
                  icon: 'success',
                  duration: 2000
                })
                self.getCollectInfo();
              }else{
                wx.showToast({
                  title: '收藏失败！',
                  icon: 'none',
                  duration: 2000
                })
              }
            })
          }
        }else{
          wx.navigateTo({
            url: `/pages/login/login`,
          })
        }
        break;
      case "order":
        if (self.data.loginInfo) {
          wx.navigateTo({
            url: `/pages/order/order?storeNo=${self.data.storeNo}&prodNo=${self.data.selectVoucher.prodNo}`,
          })
        }else{
          wx.navigateTo({
            url: `/pages/login/login`,
          })
        }
      break;
      case "evaluateList":
        wx.navigateTo({
          url: `/pages/evaluateList/evaluateList?storeNo=${self.data.storeNo}`,
        })
      break;
    }
  },
  goBack(){
    wx.navigateBack()
  },
  radioChange(e){
    let value=e.detail.value;
    self.data.vouchers.forEach(el=>{
      if (el.id == value){
        self.data.selectVoucher=el;
      }
    })
    self.setData({
      selectVoucher: self.data.selectVoucher
    })
  },
  changeShowFacilities(e){
    self.data.isShowFacilities = !self.data.isShowFacilities;
    self.setData({
      isShowFacilities: self.data.isShowFacilities
    })
  },
  previewImage(e){
    /**点击查看图片**/ 
   let index= e.currentTarget.dataset.index;
    wx.previewImage({
      current: index, // 当前显示图片的http链接
      urls: self.data.storeDtl.picList // 需要预览的图片http链接列表
    })
  }
})